/**
 *chongzhi
 */

module game {
    export class CZ_XianShangPanel extends eui.Component {
        public playLogo: eui.Image;
        public userID: eui.Label;
        public moneyNum: eui.EditableText;

        public addButtonGroup: eui.Group;
        public conmitButton: eui.Button;


        private data = null;
        private addmoney = null;//选中的金额
        private PayId = null;//渠道ID
        private Addr = null;//支付接口
        private MinAmt = null;
        private MaxAmt = null;
        private CheckAddr = null;//查询订单接口
        private PheonixNo = null;//充值订单，切换到回游戏时查询这个订单有没有支付成功
        public constructor(data: Object) {
            super();
            this.data = data;
            this.addEventListener(egret.Event.COMPLETE, this.init, this)
            this.addEventListener(egret.Event.REMOVED, this.destroy, this)
            this.skinName = "resource/ui/game/CZ_PayServerInfoCells.exml";
        }
        private init() {
            var Moneys: Array<number> = this.data["Moneys"];
            this.moneyNum.prompt = "最低充值" + Moneys[0] + "元"
            this.PayId = this.data["PayId"];
            this.Addr = this.data["Addr"]
            this.MinAmt = Moneys[0];
            this.MaxAmt = Moneys[Moneys.length - 1];
            this.CheckAddr = this.data["CheckAddr"]
            //logo
            var PayType = this.data["PayType"]
            if (PayType == "JingDong") {//京东
                this.playLogo.source = "chongzhi_jd_logo";
            } else if (PayType == "UnionPay") {//云闪付
                this.playLogo.source = "chongzhi_ysf_logo";
            } else if (PayType == "QuickPay") {//银联
                this.playLogo.source = "chongzhi_yl_logo";
            } else if (PayType == "WeiXin") {//微信
                this.playLogo.source = "chongzhi_wx_logo";
            } else if (PayType == "ZhiFuBao") {//支付宝
                this.playLogo.source = "chongzhi_zfb_logo";
            }
            this.userID.text = "当前用户ID:" + GameVO.getVo().dwUserID;
            this.addButtonGroup.removeChildren();
            //充值数额按钮
            for (var i = 0; i < Moneys.length; i++) {
                var moneyButton = new components.moneyButton();
                moneyButton.icon = "chongzhi_money_bg"
                moneyButton.wenben.text = Moneys[i] + ""
                this.addButtonGroup.addChild(moneyButton)
                moneyButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.addButtonClick, this)
            }
            this.conmitButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.conmitClick, this)
            this.moneyNum.addEventListener(egret.FocusEvent.FOCUS_IN, () => {
                this.moneyNum.text = ""
            }, this)
            this.moneyNum.addEventListener(egret.FocusEvent.FOCUS_OUT, () => {
                var text = parseInt(this.moneyNum.text)
                if (text + "" != "NaN") {
                    this.addmoney = text
                    this.moneyNum.text = text + ""
                } else {
                    this.moneyNum.text = ""
                    this.addmoney = null
                }
            }, this)
        }
        private destroy() {
            this.removeEventListener(egret.Event.REMOVED, this.destroy, this)
            var leng = this.addButtonGroup.numChildren
            for (var i = 0; i < leng; i++) {
                this.addButtonGroup.getChildAt(i).removeEventListener(egret.TouchEvent.TOUCH_TAP, this.addButtonClick, this)
            }
            this.conmitButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.conmitClick, this)
            this.moneyNum.removeEventListener(egret.FocusEvent.FOCUS_IN, () => {
                this.moneyNum.text = ""
            }, this)
            this.moneyNum.removeEventListener(egret.FocusEvent.FOCUS_OUT, () => {
                var text = parseInt(this.moneyNum.text)
                if (parseInt(this.moneyNum.text) != NaN) {
                    this.addmoney = text
                    this.moneyNum.text = text + ""
                } else {
                    this.moneyNum.text = ""
                    this.addmoney = null
                }
            }, this)
        }
        //点击选金币按钮
        private addButtonClick(eve: egret.TouchEvent) {
            var button: components.moneyButton = eve.currentTarget;
            this.addmoney = button.getShuzhi()
            this.moneyNum.text = button.getShuzhi();
            this.moneyNum.textColor = 0xEFC851;
        }
        //点击快捷支付充值提交按钮
        private conmitClick(eve: egret.TouchEvent) {
            var playtype = this.PayId;
            var addmoney = this.addmoney;
            if (playtype && addmoney) {
                if (addmoney < this.MinAmt) {
                    EffectUtils.textFadeAway("最少充值" + this.MinAmt + "元!")
                    return;
                }
                if (addmoney > this.MaxAmt) {
                    EffectUtils.textFadeAway("最多充值" + this.MaxAmt + "元!")
                    return;
                }
                var time = Global.getCurrTimeLong()
                var key = Global.getSynLoginKey(time);
                var request = new egret.HttpRequest();
                request.responseType = egret.HttpResponseType.TEXT;
                request.open(InstructionsEnum.IP_API + this.Addr + "?userid=" + GameVO.getVo().dwUserID + "&time=" + time + "&signature=" + key + "&pay_id=" + playtype + "&money=" + addmoney, egret.HttpMethod.GET);
                request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                    EffectUtils.hideWaritPanel()
                    var request = <egret.HttpRequest>event.currentTarget;
                    try {
                        var obj = JSON.parse(request.response)
                        if (obj.code == 0) {
                            this.PheonixNo = obj["PheonixNo"]
                            var url = obj["url"];
                            window.open(url)
                            egret.MainContext.instance.stage.addEventListener(egret.Event.ACTIVATE, this.findPheonixNo, this);
                        } else {
                            Global.tipShow(obj.msg)
                        }
                    } catch (err) {
                        EffectUtils.textFadeAway("数据包异常，无法解析!")
                    }
                }, this);
                request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                    EffectUtils.hideWaritPanel()
                    Global.tipShow("网络请求失败，请稍后重试!")
                }, this);
                request.send();
                EffectUtils.showWaritPanel()
            }
        }
        //查询订单
        public findPheonixNo() {
            egret.MainContext.instance.stage.removeEventListener(egret.Event.ACTIVATE, this.findPheonixNo, this);
            var time = Global.getCurrTimeLong()
            var key = Global.getSynLoginKey(time);
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + this.CheckAddr + "?userid=" + GameVO.getVo().dwUserID + "&time=" + time + "&signature=" + key + "&PheonixNo=" + this.PheonixNo, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                var request = <egret.HttpRequest>event.currentTarget;
                try {
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        game.AppFacade.instance.sendNotification(PanelNotify.HALL.FIND_USER_MONEY)
                        Global.tipShow("订单充值成功!\r本次入账" + obj["Amount"] + "元。\r感谢您对本平台的支持!")
                    } else {
                        Global.tipShow("本次充值暂未成功。\r 由于网络原因，充值可能会有3-10分钟的延迟。\r如超时仍未到账，请联系客服!")
                        // Global.tipShow(obj["msg"])
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            EffectUtils.showWaritPanel()
            request.send();
        }
    }
}